﻿using System;

namespace CardPlay.Client.Model.Persistence
{
    public class GamePlay
    {
        public Guid Id { get; set; }
        public Guid IdOfGame { get; set; }
        public int Seed { get; set; }
        public DateTime TimeStarted { get; set; }
        public int TotalSecondsInPlay { get; set; }
        public int TotalMoves { get; set; }
        public int PersistedOutcomeOfPlay { get; set; }

        public TimeSpan DurationOfPlay
        {
            get { return TimeFinished - TimeStarted; }
        }

        public DateTime TimeFinished
        {
            get { return TimeStarted.AddSeconds(TotalSecondsInPlay); }
        }

        public GameOutcome OutcomeOfPlay
        {
            get { return (GameOutcome)PersistedOutcomeOfPlay; }
            set { PersistedOutcomeOfPlay = (int) value; }
        }

        public virtual Player Player { get; set; }

        public RecordOfPlay ToRecordOfPlay()
        {
            return new RecordOfPlay
            {
                Id = Id,
                GameId = IdOfGame,
                GameNumber = Seed,
                PlayerId = Player.Id,
                TimeStarted = TimeStarted,
                Duration = TimeSpan.FromSeconds(TotalSecondsInPlay),
                TotalMoves = TotalMoves,
                Outcome = OutcomeOfPlay,
            };
        }

        public static implicit operator GamePlay(RecordOfPlay record)
        {
            return new GamePlay
            {
                Id = record.Id,
                IdOfGame = record.GameId,
                Seed = record.GameNumber,
                TimeStarted = record.TimeStarted,
                TotalSecondsInPlay = (int) record.Duration.TotalSeconds,
                TotalMoves = record.TotalMoves,
                OutcomeOfPlay = record.Outcome
            };
        }
    }
}